package app.modules.account.application.web;

import app.modules.account.business.service.UserRoleRelationService;
import app.modules.account.business.service.UserService;
import app.modules.account.dto.web.WebAssignRoleDTO;
import app.modules.account.dto.web.WebCurrentUserDTO;
import app.modules.account.dto.web.WebUserDTO;
import app.modules.account.dto.web.WebUserQueryDTO;
import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.security.dto.web.role.WebRoleDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mh.z
 */
@Tag(name = "账号/用户API")
@RestController
public class UserController {
    private final UserService userService;
    private final UserRoleRelationService userRoleRelationService;

    public UserController(UserService userService,
                          UserRoleRelationService userRoleRelationService) {
        this.userService = userService;
        this.userRoleRelationService = userRoleRelationService;
    }

    @Operation(summary = "查询用户")
    @PostMapping("/api/account/user/pageUsersByCond")
    public ResponseEntity<WebResult<List<WebUserDTO>>> pageWebUserDTOsByCond(
            @RequestBody WebUserQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebUserDTO> recordList = userService.pageWebUserDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定用户的所有已分配角色")
    @PostMapping("/api/account/user/listAssignedRoles")
    public ResponseEntity<WebResult<List<WebRoleDTO>>> listAssignedRoles(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        List<WebRoleDTO> recordList = userRoleRelationService.listAssignedResources(recordId);

        return WebResult.success(recordList).toOkEntity();
    }

    @Operation(summary = "向指定用户分配角色")
    @PostMapping("/api/account/user/assignRoles")
    public ResponseEntity<WebResult<Object>> assignResources(
            @RequestBody WebAssignRoleDTO assignDTO) {
        Long userId = assignDTO.getUserId();
        List<Long> roleIdList = assignDTO.getRoleIds();
        userRoleRelationService.assignRole(userId, roleIdList);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "返回当前用户信息")
    @PostMapping("/api/account/user/getCurrentUser")
    public ResponseEntity<WebResult<WebCurrentUserDTO>> getCurrentUser() {
        WebCurrentUserDTO userDTO = userService.getCurrentUser();

        return WebResult.success(userDTO).toOkEntity();
    }

}
